package com.jilijili.controller;

import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.pojo.bo.CommentBo;
import com.jilijili.pojo.vo.CommentVo;
import com.jilijili.service.CommentService;
import com.jilijili.service.base.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @BelongsProject: jilijili-red-book-dev
 * @BelongsPackage: com.jilijili.controller
 * @Author: ljj
 * @CreateTime: 2022-04-04  06:43
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Api(value = "评论接口", tags = "评论接口接口", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/comment")
public class CommentController extends BaseInfoProperties {

    @Resource
    private CommentService commentService;


    /**
     * 取消评论点赞
     *
     * @param userId    喜欢这条评论的用户
     * @param commentId 喜欢的评论
     */
    @ApiOperation(value = "取消评论点赞")
    @PostMapping(value = "/unlike")
    public GraceJSONResult unlike(@RequestParam(value = "userId") String userId,
                                  @RequestParam(value = "commentId") String commentId) {
        this.commentService.unlikeComment(userId, commentId);
        return GraceJSONResult.ok("操作成功");
    }

    /**
     * @param userId    喜欢这条评论的用户
     * @param commentId 喜欢的评论
     */
    @ApiOperation(value = "点赞喜欢的是视频")
    @PostMapping(value = "/like")
    public GraceJSONResult like(@RequestParam(value = "userId") String userId,
                                @RequestParam(value = "commentId") String commentId) {
        this.commentService.likeComment(userId, commentId);
        return GraceJSONResult.ok("操作成功");
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping(value = "/delete")
    public GraceJSONResult deleteComment(@RequestParam(value = "commentUserId") String commentUserId,
                                         @RequestParam(value = "commentId") String commentId,
                                         @RequestParam(value = "vlogId") String vlogId) {
        this.commentService.deleteComment(commentUserId, commentId, vlogId);
        return GraceJSONResult.ok("操作成功");
    }


    @ApiOperation(value = "查询评论")
    @GetMapping(value = "/list")
    public GraceJSONResult counts(@RequestParam(value = "vlogId") String vlogId,
                                  @RequestParam(value = "userId", defaultValue = "") String userId,
                                  @RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PagedGridResult pagedGridResult = this.commentService.queryCommentByVlodIdList(vlogId, userId, page, pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }


    @ApiOperation(value = "评论总数")
    @GetMapping(value = "/counts")
    public GraceJSONResult counts(@RequestParam(value = "vlogId") String vlogId) {
//        String commentCount = this.redis.get(REDIS_VLOG_COMMENT_COUNTS + ":" + vlogId);
        Integer commentCount = (Integer) this.redis.getRedisTemplate().opsForValue().get(REDIS_VLOG_COMMENT_COUNTS + ":" + vlogId);
        return GraceJSONResult.ok(commentCount);
    }


    /**
     * 发表评论
     *
     * @param commentBo 传输层
     */
    @ApiOperation(value = "发表评论")
    @PostMapping(value = "/create")
    public GraceJSONResult friendList(@RequestBody @Valid CommentBo commentBo) {
        CommentVo commentVo = this.commentService.createComment(commentBo);
        return GraceJSONResult.ok(commentVo);
    }
}
